<template>
  <div class="dsq-table">
    <div class="header">
      <div class="headerTitle">
        <slot name="headerTitle">
          <h3>{{ headerTitle }}</h3>
        </slot>
      </div>
      <div class="headerRefresh">
        <slot name="headerRefresh">
          <el-button type="primary" size="medium">新建用户</el-button>
          <el-button>
            <el-icon>
              <refresh />
            </el-icon>
          </el-button>
        </slot>
      </div>
    </div>
    <div class="mainContent">
      <el-table
        :data="tableData"
        style="width: 100%"
        border
        @selection-change="handleSelectionChange"
        :tree-props="childrenProps.treeProps"
        :row-key="childrenProps.rowKey"
      >
        <el-table-column
          v-if="showSelectColumn"
          type="selection"
          align="center"
          min-width="80"
        ></el-table-column>
        <el-table-column
          v-if="showIndexColumn"
          type="index"
          align="center"
          min-width="80"
          label="序号"
        ></el-table-column>
        <template v-for="item in tableColumnConfig" :key="item.prop">
          <el-table-column v-bind="item" align="center" show-overflow-tooltip>
            <template #default="scope">
              <slot :name="item.slotName" :row="scope.row">{{
                scope.row[item.prop]
              }}</slot>
            </template>
          </el-table-column>
        </template>
      </el-table>
    </div>
    <div class="footer">
      <el-pagination
        :current-page="page.currentPageNum"
        :page-sizes="[10, 20, 30]"
        :page-size="page.currentPageSum"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalData ?? 9"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps, withDefaults, defineEmits } from 'vue';
import type { IColumnType } from './type';
const props = withDefaults(
  defineProps<{
    tableData: any[];
    tableColumnConfig: IColumnType[];
    showIndexColumn?: boolean;
    showSelectColumn?: boolean;
    headerTitle?: string;
    totalData: number;
    page: any;
    childrenProps?: any;
  }>(),
  {
    showIndexColumn: true,
    showSelectColumn: true,
    headerTitle: '标题',
    childrenProps: {
      treeProps: { chidlren: 'children' },
      rowKey: 'id'
    }
  }
);
const emits = defineEmits(['selectionChange', 'update:page']);
const handleSelectionChange = (value: any) => {
  emits('selectionChange', { value });
};
const handleSizeChange = (currentPageSum: number) => {
  emits('update:page', { ...props.page, currentPageSum });
};
const handleCurrentChange = (currentPageNum: number) => {
  emits('update:page', { ...props.page, currentPageNum });
};
/*  */
</script>

<style scoped lang="less">
.dsq-table {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;

  .header {
    // height: 48px;
    padding: 10px;
    padding-bottom: 0px;
    align-items: center;
    display: flex;
    justify-content: space-between;
    background-color: #fff;
  }
  .mainContent {
    padding: 10px;
    background-color: #fff;
  }
  .footer {
    text-align: right;
    margin-top: 10px;
  }
}
</style>
